<?php
class ArticleController extends CustomControllerAction {
	protected $identity = null;
	public function init() {
		parent::init();
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$this->identity = $auth->getIdentity();
		}
		
		$this->breadcrumbs->addStep('文章', $this->getUrl('index', 'article'));
	}

	public function indexAction() {
		//TODO 文章首页，最新，最佳等排行
	}
	
	public function articlenotfoundAction() {
		//未发现时跳转
	}
	public function viewAction() {
		
		//TODO 文章浏览权限问题未解决
		$request = $this->getRequest();
		$articleid = trim($request->getQuery('id'));

        if (strlen($articleid) == 0) {
            $this->_redirect($this->getUrl('index', 'article'));
        }
        
        $article = new DatabaseObject_Article($this->db);
        if (!$article->load((int)$articleid)) {
            $this->_redirect($this->getUrl('articlenotfound', 'article'));
            return;
        }
        
		$requset = $this->getRequest();
	    
	    if (isset($this->identity)) {
	       	$userid = $this->identity->userid;
	    } else {
	    	$userid = 0;
	    }
	    $fp = new FormProcessor_RemarkEdit($this->db, $userid, $article->getId());
        if ($request->isPost()) {
            $fp->process($request);
        }
	    
        $user = new DatabaseObject_User($this->db);
        $user->load($article->userid);

	    $options = array('articleid' => $article->getId(),
                         'order'  => 'r.dateline desc'
                         );
	    $remarks = DatabaseObject_Remark::GetRemarks($this->db, $options);
		$totalRemarks = DatabaseObject_Remark::GetRemarkCount($this->db, $options);
		
		$this->breadcrumbs->addStep('文章浏览', $this->getCustomUrl(
		      array('articleid' => $article->getId())));

		$this->view->fp = $fp;
		$this->view->article = $article;
		$this->view->remarks = $remarks;
		$this->view->user = $user;
		$this->view->totalRemarks = $totalRemarks;
	}
}
?>